var deleteItem='';
var getArr=JSON.parse(localStorage.getItem('staff'));
//创建分页//
// var paging=[];
// var Mgt=Math.ceil(getArr.length/5);
var itemMax = 5;
//当前页面
var nowPage = 1;
var firstItem = itemMax * (nowPage - 1);
var lastItem = itemMax * (nowPage);
var Pd=true;
//规定一页多少项//
// function date(){
//     for(var n=1;n<Mgt+1;n++){
//        paging.push(getArr.slice((n-1)*6,6*n))
//     }
// }
// date();
//创建页码//
// function page(){
//     $('.pagination').append('<li><a href="javascript:;" class="pageNum" id="leftPage"><span style="font-size: 14px;">&laquo;</span></a></li>');
//     for(var i=1;i<Mgt+1;i++){
//         $('.pagination').append('<li><a href="javascript:;" class="pageNumI" >' + i+ '</a></li>')
//     }
//     $('.pagination').append('<li><a href="javascript:;" class="pageNum" id="rightPage"><span style="font-size: 14px;">&raquo;</span></a></li>');
//     if(i==1){
//         oLi.addClass('active');
//     }
// }
// page();
function getPageNum() {
    var pageNum=Math.ceil(getArr.length/5);
    $('.pagination').html('');
        $('.pagination').append('<li class="left"><a href="javascript:;" class="pageNum" id="leftPage"><span style="font-size: 14px;">&laquo;</span></a></li>');
    for(var i=1;i<pageNum+1;i++){
        if(i==nowPage){
            $('.pagination').append('<li class="active"><a href="javascript:;" class="pageNumI" >' + i+ '</a></li>')
        }else {
            $('.pagination').append('<li><a href="javascript:;" class="pageNumI" >' + i+ '</a></li>')
        }
    }
    $('.pagination').append('<li class="right"><a href="javascript:;" class="pageNum" id="rightPage"><span style="font-size: 14px;">&raquo;</span></a></li>');

}
getPageNum();
//创建表格//
// function Cj(){
//     $.each(paging[0], function(index, value) {
//         createList($('table1'), index, value)
//         itemResort(1);
//     })
// }
// Cj();
$.each(getArr, function(index, value) {
    if (index >= firstItem && index < lastItem) {
        createList($('tbody'), index, value);
        itemResort(1)
    }
});
// 页码点击事件//
function Dj(){
    $('.pageNumI').on('click',function () {
        $('tbody').html('');
        var Spage=$(this).text();
        $(this).parent().addClass('active').siblings('li').removeClass('active');
        nowPage = Spage;
        firstItem = itemMax * (nowPage - 1);
        lastItem = itemMax * (nowPage);
        $.each(getArr, function(index, value) {
            if (index >= firstItem && index < lastItem) {
                createList($('tbody'), index, value);
                itemResort(Spage)
            }
        });
    });
}
Dj();
function Left(){
    $('.left').on('click',function () {
        nowPage=$('.active').text()
        if(nowPage<=1){
        }else {
            $('tbody').html('');
            $('.active').prev().addClass('active').siblings('li').removeClass('active');
            firstItem = itemMax * (nowPage-2);
            lastItem = itemMax * (nowPage-1);
            $.each(getArr, function(index, value) {
                if (index >= firstItem && index < lastItem) {
                    createList($('tbody'), index, value);
                    itemResort(nowPage-1)
                }
            })
        }
    });
}
function Right(){
    $('.right').on('click',function () {
        var pageNum=Math.ceil(getArr.length/5);
        nowPage=$('.active').text();
        if(nowPage>=pageNum){
        }else {
            $('tbody').html('');
            var m=parseInt(nowPage)+1;
            $('.active').next().addClass('active').siblings('li').removeClass('active');
            firstItem = itemMax * (nowPage);
            lastItem = itemMax * (m);
            $.each(getArr, function(index, value) {
                if (index >= firstItem && index < lastItem) {
                    createList($('tbody'), index, value);
                    itemResort(m)
                }
            })
        }
    });
}
Left();
Right();
//表格排序//
function itemResort(page) {
    for (var i = 0; i < $('tbody').children().length; i++) {
        if (i >=0) {
            $('tbody').children().eq(i).children().eq(0).text((page-1)*5+i+1);
        }
    }
}
function createList(getArr, index, value) {
    $('tbody').append('<tr><td style="vertical-align: middle"></td><td>' +'<img src="'+value.src+'"  class="img-rounded" style="width: 50px;height: 50px">' + '</td><td style="vertical-align: middle">' + value.name + '</td><td style="vertical-align: middle">'+value.department+'</td><td style="vertical-align: middle">'+ value.post + '</td><td style="vertical-align: middle">' + value.sex+ '</td><td style="vertical-align: middle">' + value.birthday + '</td><td style="vertical-align: middle">'
        +value.Tel +
        '</td><td style="vertical-align: middle">'+value.Jn+'</td><td style="vertical-align: middle"><div class="Staff-operate">'+
        '<button type="button" class="btn btn-primary btn-sm edit"  data-toggle="modal" data-target="#StaffEdit">编辑</button>' +
        '<button type="button" class="btn btn-warning btn-sm delete"  data-toggle="modal" data-target="#StaffDelete">删除</button>' +
        '</div></td></tr>'
    );
}
//年月日//
var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var MonDay=[1,2,3,4,5,6,7,8,9,10,11,12];
var oDate= new Date().getFullYear();
//年份//
for(var i=oDate-60;i<oDate-18;i++){
    $('.year').append('<li><a href="javascript:;">' + i + '</a></li>');
}
//月份//
for(var i=0;i<MonDay.length;i++){
    $('.mon').append('<li><a href="javascript:;">' + MonDay[i] + '</a></li>');
}
//日期//
function days(){
    var k=$('.AddMon').text();
    var m=$('.EditMon').text();
    $('.Addday').html('');
    $('.Editday').html('');
    for(i=1;i<MonHead[k-1]+1;i++){
        $('.Addday').append('<li><a href="javascript:;">'+i+ '</a></li>')
    }
    for(i=1;i<MonHead[m-1]+1;i++){
        $('.Editday').append('<li><a href="javascript:;">'+i+ '</a></li>')
    }
}
$('.EditDay').on('click',function () {
    days();
});
$('.AddDay').on('click',function () {
    days();
});
//员工部门//
var getBrr=JSON.parse(localStorage.getItem('departmentJson'));
//获取部门名称
var getoBrr=[];
for(var i=0;i<getBrr.length;i++){
    getoBrr.push(getBrr[i].name);
}
//添加部门
for(var i=0;i<getBrr.length;i++){
    $('.department').append('<li><a href="javascript:;">' + getoBrr[i] + '</a></li>');
}
//员工职位//
var post=['园长','教导主任','年级主任','数学老师','语文老师','美术老师','生活老师','体育老师','保洁','保安','HR'];
for(var i=0;i<post.length;i++){
    $('.post').append('<li><a href="javascript:;">' + post[i] + '</a></li>');
}
//下拉框替换//
for(var i=0;i<$('.myDropdown').length;i++){
    (function (k) {
        $('.myDropdown').eq(k).on('click','li',function (e) {
            if(k==4 || k==10){
                $('.AddDay').html('1<span class="caret"></span>');
                $('.EditDay').html('1<span class="caret"></span>');
                $('.dropdown-toggle').eq(k).html(e.target.innerText + '<span class="caret"></span>')
            } else {
                $('.dropdown-toggle').eq(k).html(e.target.innerText + '<span class="caret"></span>');
            }
            console.log(k)
            }
        )
    })(i);
}
// 指定每行的按钮将要操作哪行
$('tbody').on('click','button',function(){
     deleteItem=$(this).parents('tr');
    if($(this).hasClass('edit')){
        var dob=$(deleteItem).children().eq(6).text();
        var Dob=dob.split(".");
        $('.addStaffRemind').css('display','none');
        $('#editStaffName').val(deleteItem.children().eq(2).text());
        $('.EditDepartment').html(deleteItem.children().eq(3).text()+'<span class="caret"></span>');
        $('.EditPost').html(deleteItem.children().eq(4).text()+'<span class="caret"></span>');
        $('.EditSex').html(deleteItem.children().eq(5).text()+'<span class="caret"></span>');
        $('.EditYear').html(Dob[0]+'<span class="caret"></span>');
        $('.EditMon').html(Dob[1]+'<span class="caret"></span>');
        $('.EditDay').html(Dob[2]+'<span class="caret"></span>');
        $('#editStaffLeaderPhone').val(deleteItem.children().eq(7).text());
        $('.EditJn').text(deleteItem.children().eq(8).text());
    }
});
//删除//
$('#deletesResure').on('click',function(){
    var m=($(deleteItem).children().eq(8).text());
    var k=$('tbody').children('tr').length;
    nowPage=$('.active').text();
    $(deleteItem).remove();
    for(var i=0;i<getArr.length;i++){
        if(getArr[i].Jn==m){
            getArr.splice(i,1);
        }
    }
    localStorage.setItem('staff',JSON.stringify(getArr));
    if(Pd){
        $('tbody').html('');
        getPageNum();
        Dj();
        Left();
        Right();
        firstItem = itemMax * (nowPage - 1);
        lastItem = itemMax * (nowPage);
        $.each(getArr, function(index, value) {
            if (index >= firstItem && index < lastItem) {
                createList($('tbody'), index, value);
                itemResort(nowPage)
            }
        });
    }
    if(k-1<=0 && nowPage>1){
        $('tbody').html('');
        getPageNum();
        Dj();
        Left();
        Right();
        $('.pagination').children().eq(nowPage-1).addClass('active').siblings('li').removeClass('active')
        firstItem = itemMax * (nowPage - 2);
        lastItem = itemMax * (nowPage-1);
        $.each(getArr, function(index, value) {
            if (index >= firstItem && index < lastItem) {
                createList($('tbody'), index, value);
                itemResort(nowPage-1)
            }
        });
    }
    $('#StaffDelete').modal('hide');
});
// 正则验证//
$('#addStaffLeader').on('blur',function () {
    var str=$(this).val();
    var judge=$(this).parent().parent().children('p');
    if(!(nameReg.test(str)) && ($(this).val()!='')){
        $(judge).css('display','block')
    }else {
        $(judge).css('display','none')
    }
});
$('#addStaffNumber').on('blur',function () {
    var str=$(this).val();
    var judge=$(this).parent().parent().children('p');
    if(!(phoneNumberReg.test(str)) && ($(this).val()!='')){
        $(judge).css('display','block')
    }else {
        $(judge).css('display','none')
    }
});
$('#editStaffName').on('blur',function () {
    var str=$(this).val();
    var judge=$(this).parent().parent().children('p');
    if(!(nameReg.test(str)) && ($(this).val()!='')){
        $(judge).css('display','block')
    }else {
        $(judge).css('display','none')
    }
});
$('#editStaffLeaderPhone').on('blur',function () {
    var str=$(this).val();
    var judge=$(this).parent().parent().children('p');
    if(!(phoneNumberReg.test(str)) && ($(this).val()!='')){
        $(judge).css('display','block')
    }else {
        $(judge).css('display','none')
    }
});
//添加//
$('.Add').on('click',function () {
     Jnm=getArr[getArr.length-1].Jn;
    var m=parseInt(Jnm)+1;
    $('.addStaffRemind').css('display','none');
    $('.AddJn').text(m);
     $('#addStaffLeader').val('');
    $('.AddDepartment').html('教育部<span class="caret"></span>');
    $('.AddPost').html('数学老师<span class="caret"></span>');
    $('.AddSex').html('男<span class="caret"></span>');
    $('.AddYear').html('1999<span class="caret"></span>');
    $('.AddMon').html('1<span class="caret"></span>');
    $('.AddDay').html('1<span class="caret"></span>');
    $('#addStaffNumber').val('');
});
$('.AddTo').on('click',function () {
    var N=$('#addStaffLeader').val();
    var D=$('.AddDepartment').text();
    var P=$('.AddPost').text();
    var S=$('.AddSex').text();
    var y=$('.AddYear').text();
    var m=$('.AddMon').text();
    var d=$('.AddDay').text();
    var T=$('#addStaffNumber').val();
    var J=$('.AddJn').text();
    if((phoneNumberReg.test(T)) && (nameReg.test(N))){
        $('tbody').append('<tr><td style="vertical-align: middle">'+'</td><td style="vertical-align: middle">' +'<img src="'+'../images/photo/0.jpg'+'"  class="img-rounded" style="width: 50px;height: 50px">' + '</td><td style="vertical-align: middle">' + N + '</td><td style="vertical-align: middle">'+ D+ '</td><td style="vertical-align: middle">'+ P+ '</td><td style="vertical-align: middle">' + S+ '</td><td style="vertical-align: middle">' + y+'.'+m+'.'+d + '</td><td style="vertical-align: middle">' +T+
            '</td><td style="vertical-align: middle">'+J+'</td><td style="vertical-align: middle"><div class="department-operate">'+
            '<button type="button" class="btn btn-primary btn-sm edit" id="edit" data-toggle="modal" data-target="#StaffEdit">编辑</button>' +
            '<button type="button" class="btn btn-warning btn-sm delete" id="delete" data-toggle="modal" data-target="#StaffDelete">删除</button>' +
            '</div></td></tr>'
        );
        getArr.push({src:'../images/photo/0.jpg',
            name: N,
            post: P,
            sex:S,
            department:D,
            birthday:y+'.'+m+'.'+d,
            Tel: T,
            password:'123456',
            introduce:'Fighting for tommorow!',
            Jn:J,
            paimanage:{
                attendance:false,
                classadmin:false,
                depManage:false,
                notice:true,
                staff:false,
                personal:true,
                studamin:false,
                paimanage:false,
            },
            kaoqin:{
                thisMonthPunctual:0,
                thisMonthSick:0,
                thisMonthLeave:0,
                thisMonthDelay:0,
                lastMonthPunctual:0,
                lastMonthSick:0,
                lastMonthLeave:0,
                lastMonthDelay:0,
            }});
        localStorage.setItem('staff',JSON.stringify(getArr));
        if(Pd){
            $('tbody').html('');
             nowPage=$('.active').text();
            $('.active').siblings('li').removeClass('active')
            getPageNum();
            Dj();
            firstItem = itemMax * (nowPage- 1);
            lastItem = itemMax * (nowPage);
            $.each(getArr, function(index, value) {
                if (index >= firstItem && index < lastItem) {
                    createList($('tbody'), index, value);
                    itemResort(nowPage)
                }
            });
        }
        $("#addStaffModel").modal('hide');
    }else{
        alert('名字或电话错误！')
    }
    Left();
    Right();
});
// 重置
$('.AddReset').on('click',function () {
    $('#addStaffLeader').val('');
    $('.AddDepartment').html('教育部<span class="caret"></span>');
    $('.AddPost').html('数学老师<span class="caret"></span>');
    $('.AddSex').html('男<span class="caret"></span>');
    $('.AddYear').html('1999<span class="caret"></span>');
    $('.AddMon').html('1<span class="caret"></span>');
    $('.AddDay').html('1<span class="caret"></span>');
    $('#addStaffNumber').val('');
});
// 编辑//
$('#edit').on('click',function () {
    var N=$('#editStaffName').val();
    var D=$('.EditDepartment').text();
    var P=$('.EditPost').text();
    var S=$('.EditSex').text();
    var y=$('.EditYear').text();
    var m=$('.EditMon').text();
    var d=$('.EditDay').text();
    var T=$('#editStaffLeaderPhone').val();
        if((phoneNumberReg.test(T)) && (nameReg.test(N))){
            var n=$(deleteItem).children().eq(0).text();
            $(deleteItem).children().eq(2).text(N);
            $(deleteItem).children().eq(3).text(D);
            $(deleteItem).children().eq(4).text(P);
            $(deleteItem).children().eq(5).text(S);
            $(deleteItem).children().eq(6).text(y+'.'+m+'.'+d);
            $(deleteItem).children().eq(7).text(T);
            $("#StaffEdit").modal('hide');
            getArr[n-1].name=N;
            getArr[n-1].post=P;
            getArr[n-1].sex=S;
            getArr[n-1].department=D;
            getArr[n-1].birthday=y+'.'+m+'.'+d;
            getArr[n-1].Tel= T;
            localStorage.setItem('staff',JSON.stringify(getArr))
        }else {
            alert('名字或电话错误！')
        }

    });
// 导出Excel
$(".export").on('click',function () {
    //列标题，逗号隔开，每一个逗号就是隔开一个单元格
    var str = '姓名,部门,职位,性别,出生日期,联系方式,工号\n';
    //增加\t为了不让表格显示科学计数法或者其他格式
    getExport=[];
    for(i=0;i<getArr.length;i++){
        getoExport={
            name:getArr[i].name,
            department:getArr[i].department,
            post:getArr[i].post,
            sex:getArr[i].sex,
            birthday:getArr[i].birthday,
            Tel:getArr[i].Tel,
            Jn:getArr[i].Jn,
        }
        getExport.push(getoExport)
    }
        for(var i = 0 ; i < getExport.length ; i++ ){
            for(var name in getExport[i]){
                str+=`${getExport[i][name] + '\t'},`;
            }
            str+='\n';
        }
    //encodeURIComponent解决中文乱码
    var uri = 'data:text/xls;charset=utf-8,\ufeff' + encodeURIComponent(str);
    //通过创建a标签实现
    var link = document.createElement("a");
    link.href = uri;
    //对下载的文件命名
    link.download =  "员工数据表.xls";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
});
// 搜索
$('.con').on('click',function () {
    var SearchName=$('#Search').val();
    $('tbody').html('');
    $('.pagination').html('');
    Pdd=true;
    $.each(getArr, function (index, value) {
        if (SearchName == value.name || SearchName == value.Jn || SearchName == value.post) {
            Pd=false;
            createList($('tbody'), index, value);
            itemResort(1);
            Pdd=false
        }});
if(Pdd){
    Pdd=false;
    $('tbody').append('<tr style="font-size: 30px"><td colspan="10">没有查到相关信息</td></tr>')
    }
});